﻿using System;
using System.Collections.Generic;
using System.Linq;
using Harness;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SHDocVw;
using mshtml;

namespace Harness.Test
{
    [TestClass()]
    public class HarnessTest
    {
        private static IHarness h = Factory.CreateHarness();

        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 追加のテスト属性
        // 
        //テストを作成するときに、次の追加属性を使用することができます:
        //
        //クラスの最初のテストを実行する前にコードを実行するには、ClassInitialize を使用
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            h.Command("close", "all");
            h.ElementHighlighted += new ElementHighlightEventHandler(h_ElementHighlighted);
        }

        static int autoScreenshotCounter = 0;

        static void h_ElementHighlighted(object sender, IHTMLElement element, System.Drawing.Rectangle rect, string cmd)
        {
            System.Diagnostics.Debug.Print("Highlight ={0}\t{1}\t{2}", element, rect, cmd);
            //string filename = string.Format(@"C:\Users\Public\Pictures\auto-screenshot-{0}.jpg", ++autoScreenshotCounter);
            //h.Command("screenshot", "save", filename);

        }
        //
        //クラスのすべてのテストを実行した後にコードを実行するには、ClassCleanup を使用
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
        }
        //
        //各テストを実行する前にコードを実行するには、TestInitialize を使用
        [TestInitialize()]
        public void MyTestInitialize()
        {
            h.Command("close", "all");
        }

        //各テストを実行した後にコードを実行するには、TestCleanup を使用
        [TestCleanup()]
        public void MyTestCleanup()
        {
        }

        #endregion



        [TestMethod()]
        public void HarnessConstructorTest()
        {
            //dynamic harness = Activator.CreateInstance(Type.GetTypeFromProgID("Harness"));

            IHarness harness = Factory.CreateHarness();

            string harnessVersion = harness.Version.ToString();
            System.Diagnostics.Debug.Print("Harness Version ={0}", harnessVersion);

            string v = harness.Command("getVersion").ToString();
            System.Diagnostics.Debug.Print("IE Version ={0}", v);

            harness.Command("newBrowser", "http://www.google.com", "-private");
        }

        [TestMethod()]
        public void FrameSwitchToTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/frame/index.html", "-private");
            h.Command("switchFrame", "main");
            IHTMLDocument2 doc = h.Command("getActiveDocument");

            Assert.AreEqual(doc.url, "http://i-harness.com/test/frame/main.html");
        }


        [TestMethod()]
        public void FrameSwitchTo2Test()
        {
            h.Command("newBrowser", "http://i-harness.com/test/frame/index.html", "-private");
            h.Command("switchFrame", "main");

            IHTMLDocument2 doc = h.Command("getActiveDocument");
            Assert.AreEqual(doc.url, "http://i-harness.com/test/frame/main.html");

            h.Command("switchFrame", "_parent");
            h.Command("switchFrame", "header");
            IHTMLDocument2 doc2 = h.Command("getActiveDocument");
            Assert.AreEqual(doc2.url, "http://i-harness.com/test/frame/header.html");

        }

        [TestMethod()]
        public void FrameSwitchToParentTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/frame/index.html", "-private");
            h.Command("switchFrame", "main");

            IHTMLDocument2 doc = h.Command("getActiveDocument");
            Assert.AreEqual(doc.url, "http://i-harness.com/test/frame/main.html");

            h.Command("switchFrame", "_parent");

            IHTMLDocument2 doc2 = h.Command("getActiveDocument");
            Assert.AreEqual(doc2.url, "http://i-harness.com/test/frame/index.html");
        }

        [TestMethod()]
        public void FrameSwitchToTopTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/frame/index.html", "-private");
            h.Command("switchFrame", "main");

            IHTMLDocument2 doc = h.Command("getActiveDocument");
            Assert.AreEqual(doc.url, "http://i-harness.com/test/frame/main.html");

            h.Command("switchFrame", "_top");

            IHTMLDocument2 doc2 = h.Command("getActiveDocument");
            Assert.AreEqual(doc2.url, "http://i-harness.com/test/frame/menu.html");
        }

        [TestMethod()]
        public void GetUrlTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");
            string url = h.Command("getUrl");

            Assert.IsTrue(url.StartsWith("http://i-harness.com/test"));
        }

        [TestMethod()]
        public void GetTitleTest()
        {
            InternetExplorer ie = h.Command("newBrowser", "http://i-harness.com/test/", "-private");
            IHTMLDocument2 doc = ie.Document;

            Assert.AreEqual(doc.title, "Test");
        }

        [TestMethod()]
        public void GoBackCommandTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com/", "-private");
            h.Command("click", "button=Search");
            h.Command("goBack");

            string url = h.Command("getUrl");
            Assert.IsTrue(url.StartsWith("http://www.yahoo.com"));
        }

        [TestMethod()]
        public void GoForwardCommandTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("click", "button=Search");
            h.Command("goBack");
            h.Command("goForward");

            string url = h.Command("getUrl");
            Assert.IsTrue(url.StartsWith("http://search.yahoo.com"));
        }

        [TestMethod()]
        public void RefreshTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");
            h.Command("refresh");
        }

        [TestMethod()]
        public void CloseTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("newBrowser", "http://www.microsoft.com", "-private");
            h.Command("close");

            IEnumerable<InternetExplorer> ie = h.Command("findBrowser");
            Assert.AreEqual(1, ie.Count());
        }

        [TestMethod()]
        public void CloseTabTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("newTab", "http://www.microsoft.com");
            h.Command("close");

            IEnumerable<InternetExplorer> ie = h.Command("findBrowser");
            Assert.AreEqual(1, ie.Count());
        }

        [TestMethod()]
        public void CloseAllTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("close", "all");

            IEnumerable<InternetExplorer> ie = h.Command("findBrowser");
            Assert.AreEqual(0, ie.Count());
        }

        [TestMethod()]
        public void SednKeysText()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("setFocus", "name=p");
            h.Command("sendKeys", "foobar{ENTER}");
            h.Command("assert", "value", "name=p", "foobar");
        }

        [TestMethod()]
        public void getActiveBrowserTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("newBrowser", "http://www.msn.com", "-private");

            InternetExplorer ie = h.Command("getActiveBrowser");
            string url = ie.LocationURL;
            Assert.IsTrue(url.StartsWith("http://www.msn.com"));
        }

        [TestMethod()]
        public void setActiveBrowserTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            InternetExplorer ie = h.Command("newBrowser", "http://www.microsoft.com/", "-private");
            h.Command("newBrowser", "http://www.msn.com", "-private");

            h.Command("setActiveBrowser", ie);

            InternetExplorer ie2 = h.Command("getActiveBrowser");
            Assert.IsNotNull(ie2);
            string url = ie2.LocationURL;
            Assert.IsTrue(url.StartsWith("http://www.microsoft.com/"));
        }

        [TestMethod()]
        public void setActiveBrowserByTabTest()
        {
            InternetExplorer yahoo = h.Command("newBrowser", "http://www.yahoo.com", "-private");
            InternetExplorer ms = h.Command("newTab", "http://www.microsoft.com/");
            InternetExplorer msn = h.Command("newBrowser", "http://www.msn.com", "-private");

            h.Command("setActiveBrowser", ms);

            InternetExplorer ie2 = h.Command("getActiveBrowser");
            Assert.IsNotNull(ie2);
            string url = ie2.LocationURL;
            Assert.IsTrue(url.StartsWith("http://www.microsoft.com/"));
        }

        [TestMethod()]
        public void NewBrowserTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("newBrowser", "http://www.msn.com", "-private");
        }

        [TestMethod()]
        public void NewTabTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("newTab", "http://www.google.com");
        }

        [TestMethod()]
        public void ResizeTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");
            h.Command("resize", 800, 600);
        }

        [TestMethod()]
        public void YahooSearchTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("set", "name=p", "harness");
            h.Command("click", "button=Search");
            h.Command("assert", "textContains", "harness");
        }

        [TestMethod()]
        public void WaitTextContainsTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");
            h.Command("click", "#wait-text-present");
            h.Command("wait", "textContains", "HELLO");
        }

        [TestMethod()]
        public void WaitElementPresentTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");
            h.Command("click", "#wait-element-present");
            h.Command("wait", "elementPresent", "#present");
        }

        [TestMethod()]
        public void WaitElementVisibleTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");
            h.Command("click", "#wait-element-show");
            h.Command("wait", "elementVisible", "#wait");
        }

        [TestMethod()]
        public void DialogScriptErrorTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("setScriptErrorLogDirectory", @"C:\Users\Public\Pictures");
            h.Command("click", "button=Error");
        }

        [TestMethod()]
        public void DialogAlertTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("dialog", "alert", "close", @"C:\Users\Public\Pictures\alert.jpg");
            h.Command("click", "#alert");
        }

        [TestMethod()]
        public void DialogConfirmOkTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("dialog", "confirm", "ok", @"C:\Users\Public\Pictures\confirm-ok.jpg");
            h.Command("click", "#confirm");
        }

        [TestMethod()]
        public void DialogConfirmCancelTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("dialog", "confirm", "cancel", @"C:\Users\Public\Pictures\confirm-cancel.jpg");
            h.Command("click", "#confirm");
        }

        [TestMethod()]
        public void DialogPromptOKTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("dialog", "prompt", "ok", "GOOD!", @"C:\Users\Public\Pictures\prompt-ok.jpg");
            h.Command("click", "#prompt");
        }

        [TestMethod()]
        public void DialogPromptCancelTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("dialog", "prompt", "cancel", string.Empty, @"C:\Users\Public\Pictures\prompt-cancel.jpg");
            h.Command("click", "#prompt");
        }

        [TestMethod()]
        public void DialogAuthTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("dialog", "auth", "test", "testtest", @"C:\Users\Public\Pictures\auth.jpg");
            h.Command("click", "link=Auth");
        }

        [TestMethod()]
        public void DialogFileSelectTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("dialog", "fileSelect", @"C:\Users\Public\Documents\test.txt", @"C:\Users\Public\Pictures\file-select.jpg");
            h.Command("click", "name=fl");
        }

        [TestMethod()]
        public void DialogFileDownloadSaveTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("dialog", "fileDownload", "save", @"C:\Users\Public\Pictures\download-save.jpg");

            System.Diagnostics.Debug.Print("click before");
            h.Command("click", "link=download.zip");
            System.Diagnostics.Debug.Print("click after");
            System.Threading.Thread.Sleep(10000);
        }

        [TestMethod()]
        public void DialogFileDownloadCancelTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("dialog", "fileDownload", "cancel", @"C:\Users\Public\Pictures\download-cancel.jpg");

            System.Diagnostics.Debug.Print("click before");
            h.Command("click", "link=download.zip");
            System.Diagnostics.Debug.Print("click after");
            System.Threading.Thread.Sleep(10000);
        }

        [TestMethod()]
        public void DownloadTest()
        {
            h.Command("download", "http://i-harness.com/test/download.zip", @"C:\Users\Public\Documents\download.zip");
        }

        [TestMethod()]
        public void SetGetTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("set", "name=t1", "foo");
            h.Command("set", "name=t2", "bar");

            string t1 = h.Command("get", "name=t1");
            Assert.AreEqual(t1, "foo");

            string t2 = h.Command("get", "name=t2");
            Assert.AreEqual(t2, "bar");
        }

        [TestMethod()]
        public void SelectTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("select", "name=se1", "label=option1");
            h.Command("select", "name=se1", "label=option2");
        }

        [TestMethod()]
        public void Select2Test()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("select", "name=se2", "value=2");
        }

        [TestMethod()]
        public void Select3Test()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("select", "name=se3", "label=option1");
            h.Command("select", "name=se3", "label=option2");
            h.Command("select", "name=se3", "label=option3");
        }

        [TestMethod()]
        public void CheckboxTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("check", "label=London");
            h.Command("check", "name=newyork");
        }

        [TestMethod()]
        public void Checkbox2Test()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("check", "label=London", true);
            bool b1 = h.Command("verify", "check", "label=London", true);
            Assert.IsTrue(b1);

            h.Command("check", "label=London", false);
            bool b2 = h.Command("verify", "check", "label=London", false);
            Assert.IsTrue(b2);
        }

        [TestMethod()]
        public void RadioTest()
        {
            h.Command("newBrowser", "http://i-harness.com/test/", "-private");

            h.Command("check", "name=rd");
            h.Command("check", "label=Red");
        }

        [TestMethod()]
        public void ScreenshotCopyTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("screenshot", "copy");
        }

        [TestMethod()]
        public void ScreenshotSaveTest()
        {
            h.Command("newBrowser", "http://www.yahoo.com", "-private");
            h.Command("screenshot", "save", @"C:\Users\Public\Documents\test.png");
        }

        [TestMethod()]
        public void YUI_TodoTest()
        {
            h.Command("newBrowser", "http://yuilibrary.com/yui/docs/app/app-todo.html");

            h.Command("set", "#new-todo", "foo");
            h.Command("sendKeys", "{ENTER}");
            h.Command("assert", "elementTextContains", "span.todo-remaining", "1");

            h.Command("set", "#new-todo", "bar");
            h.Command("sendKeys", "{ENTER}");
            h.Command("assert", "elementTextContains", "span.todo-remaining", "2");

            h.Command("check", "input.todo-checkbox");
            h.Command("click", "a.todo-remove");
            h.Command("assert", "elementTextContains", "span.todo-remaining", "1");

            h.Command("check", "input.todo-checkbox");
            h.Command("click", "a.todo-remove");
            h.Command("assert", "!elementPresent", "span.todo-remaining");
        }
    }
}
